package com.ytd.smartcanteen.weighting.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ytd.smartcanteen.weighting.bean.MealTimeInfo;
import com.ytd.smartcanteen.weighting.util.FoodManager;

import java.util.List;

import cn.ytd.smartcanteen.weighting.R;

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private final Context myContext;
    private List<MealTimeInfo> gData;
    private int mGroupPosition = -1, mChildPosition = -1;

    public ExpandableAdapter(Context mContext, List<MealTimeInfo> gData) {
        this.myContext = mContext;
        this.gData = gData;
    }

    public void setDataList(final List<MealTimeInfo> mDataList) {
        this.gData = mDataList;
        notifyDataSetChanged();
    }

    public void setSelectPosition(int mGroupPosition, int mChildPosition) {
        this.mGroupPosition = mGroupPosition;
        this.mChildPosition = mChildPosition;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return gData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return gData.get(groupPosition).mMealCategoryInfos.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return gData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return gData.get(groupPosition).mMealCategoryInfos.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.item_exlist_group, parent, false);
            holderGroup = new ViewHolderGroup();
            holderGroup.tv_img = (ImageView) convertView.findViewById(R.id.tv_group_img);
            holderGroup.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            holderGroup.tv_group_icon = (ImageView) convertView.findViewById(R.id.tv_group_icon);
            convertView.setTag(holderGroup);
        } else {
            holderGroup = (ViewHolderGroup) convertView.getTag();
        }
        if (FoodManager.getInstance().isMealTimeBound(gData.get(groupPosition).mealTimeId)) {
            holderGroup.tv_img.setImageDrawable(myContext.getDrawable(R.drawable.bd));
        } else {
            holderGroup.tv_img.setImageDrawable(null);
        }
        holderGroup.tv_group_name.setText(gData.get(groupPosition).mealTimeName);
        if (gData.get(groupPosition).mMealCategoryInfos.size() > 1) {
            if (isExpanded) {
                holderGroup.tv_group_icon.setImageResource(R.drawable.zcfl);
            } else {
                holderGroup.tv_group_icon.setImageResource(R.drawable.zcfl);
            }
        }
        return convertView;

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        ViewHolderItem holderItem;
        if (convertView == null) {
            convertView = LayoutInflater.from(myContext).inflate(R.layout.item_exlist_item, parent, false);
            holderItem = new ViewHolderItem();
            holderItem.tv_name = convertView.findViewById(R.id.tv_name);
            holderItem.tv_img = convertView.findViewById(R.id.tv_img);
            convertView.setTag(holderItem);
        } else {
            holderItem = (ViewHolderItem) convertView.getTag();
        }
        holderItem.tv_name.setText(gData.get(groupPosition).mMealCategoryInfos.get(childPosition).categoryName);

        if (FoodManager.getInstance().isCategoryBound(gData.get(groupPosition).mealTimeId,
            gData.get(groupPosition).mMealCategoryInfos.get(childPosition).id)) {
            holderItem.tv_img.setImageDrawable(myContext.getDrawable(R.drawable.bd));
        } else {
            holderItem.tv_img.setImageDrawable(null);
        }
        if (mGroupPosition == groupPosition && mChildPosition == childPosition) {
            holderItem.tv_name.setTextColor(Color.parseColor("#0e88fc"));
        } else {
            holderItem.tv_name.setTextColor(Color.parseColor("#000000"));
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup {
        private ImageView tv_img;
        private TextView tv_group_name;
        private ImageView tv_group_icon;
    }

    private static class ViewHolderItem {
        private TextView tv_name;
        private ImageView tv_img;
    }

}

